<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 17-5-2
 * Time: 下午3:38
 */

namespace App\Controller;

use Think\Controller;
class CartController extends Controller{

    public function del()
    {
        $model=M('Cart');
        $model->where(['member_id'=>1])->delete();
        $this->success('删除购物车成功','',1);
    }

    public function add()
    {
    if(IS_POST){
    //dump($_POST);die;
    $model=D('Cart');
    $gn=I('post.goods_number',0,'intval');
    $price=I('post.price',0,'intval');
    if(empty($price)){
        $this->error('请选择菜后才提交');
    }
        foreach($gn as $k=>$v){
                if(empty($v)){
                    continue;
                }else{
                    $cmodel=M('Goods');
                    $pri = $cmodel->where(['id'=>$k])->getField('price');
                    $has=$model->where(['goods_id'=>$k])->find();
                    if($has){
                        $model->where(['goods_id'=>$k])->setInc('goods_number',$v);
                        $model->where(['goods_id'=>$k])->setInc('price',$pri * $v);
                        $this->success('加入购物车成功',U('Index/submit'));
                    }else{
                        $data[]=array(
                            'goods_id'=>$k,
                            'goods_number'=>$v,
                            'price'=>$pri * $v,
                            'member_id'=>1
                        );
                    }
                }
        }
        if($model->addAll($data)){
<<<<<<< HEAD
            $this->success('加入购物车成功',U('submit'),1);
=======
            $this->success('加入购物车成功',U('Index/submit'));
>>>>>>> a8dd213a32fe18268995d72a76272b94cad6545e
        }else{
            $this->error('加入购物车失败');
        }
    }
    }

    public function submit()
    {
        $cmodel=D('Cart');
        $model=D('Order');
        $amodel=M('Address');
        if(IS_POST){

            //dump($_POST);exit;

        $order=$cmodel->where(['member_id'=>1])->select();
            $remark=I('post.remark');
            $people=I('post.people');
            $aid=I('post.aid', 0, 'intval');
            $price=I('post.price');
            if(empty($people)){
                $this->error('请选择就餐人数');
            }
            if($aid >0 ){

                $tel=$amodel->where(['id'=>$aid])->getField('tel');
                $address=$amodel->where(['id'=>$aid])->getField('address');
            }else{
//新建一个收获地址，为何添加上去，电话号码依然是根据address表里查出来的电话呢？
                $address=I('post.address');
                $tel=I('post.tel');
                $name=I('post.name');
                if(empty($name)){
                    $this->error('请填写姓名');
                }
                if(empty($address)){
                    $this->error('请填写地址');
                }
                if(!preg_match("/^1[34578]{1}\d{9}$/",$tel)){
                    $this->error('请填写电话或者电话格式不正确');
                }


                $data=array(
                    'uid'=>1,
                    'name'=>$name,
                    'tel'=>1111111111,
                    'address'=>$address
                );
                //dump($data);exit;     这个地方都是正确的电话号码，为何到数据库就错了呢？？？？？？

                $amodel->data($data)->add();
            }


            $goods=array();
            $num=array();
            foreach($order as $k=>$v){

                $_goods[]=$v['goods_id'];
                $_num[]=$v['goods_number'];
            }
            $goods=implode(',',$_goods);
            $num=implode(',',$_num);

            if($model->add(
                array(
                    'goods_id'=>$goods,
                    'goods_number'=>$num,
                    'tel'=>$tel,
                    'remark'=>$remark,
                    'people'=>$people,
                    'addtime'=>time(),
                    'order_num'=>'BX'.rand(1000000000,9000000000),
                    'price'=>$price+10,
                    'address'=>$address,
                    'status'=>2,
                    'member_id'=>1,
                    'aid'=>$aid
                )
            )){
                $cmodel->where(['member_id'=>1])->delete();


                $this->success('付单成功',U('Index/add'));
            }else{
                $this->error('付单失败');
            }
        }else{
            $datas=$cmodel->alias('a')->field('a.goods_number,a.price,b.gname,b.image')
                ->join('__GOODS__ b ON a.goods_id=b.id')
                ->where(['member_id'=>1])
                ->select();
            $money=$cmodel->sum('price');
            $this->datas=$datas;
            $this->money=$money;
            $adr=$amodel->where(['uid'=>1])->limit(5)->order('id desc')->select();
            $this->adr=$adr;

            $this->display();
        }

    }



} 